var add,edit,setVal,vrsnType;
require(['select2', 'date','validator','jquery-form'], function() {
    var cnt = 0,$formData = $('#formData'),$tab=$("#tab"),paramHtml;
    $formData.initForm();

    add = function(fn) {
        addOrUpdate("/upgrade/addUpgradeFile.htm",fn);
    };

    edit = function(fn) {
        addOrUpdate("/upgrade/uptUpgradeFile.htm",fn);
    };

    reset = function (vrsnType,vrsnTypeName){
        vrsnType = vrsnType;
        $formData.reset($formData);
        $("#vrsnType").val(vrsnType)
        $("#vrsnTypeName").val(vrsnTypeName)
        $.ajaxExt({
            url :"/upgrade/getParamByVrsnType.htm",
            data : {'vrsnType' : vrsnType}
        }).done(function(data){
            paramHtml="";
            $(data).each(function(i,item) {
                paramHtml+='<option value="'+item.paramId+'"'+(i===0?"selected":"")+'>'+item.paramLevel2Name+'</option>';
            });
            $("#paramId").html(paramHtml);
        });
        $formData.initForm();
    };

    setVal = function (vrsnId){
        $.ajaxExt({
            url :"/upgrade/getDetailByVrsnId.htm",
            data : {'vrsnId' : vrsnId}
        }).done(function(data){
            $("#tab tr:not(:first)").remove();
            $(data).each(function(i,item) {
                var newTR = "<tr id='newTR'>"
                    + "<td style='border-left: 1px solid #DCDCDC' class='ac'><input class='input-text input-table' type='text' value='"+item.paramName+"'></td>"
                    + "<td class='ac'><input class='input-text input-table' type='text' value='"+item.paramValue+"'></td>"
                    + "</tr>";
                // $("#endTR").after(newTR);
                $("#tab tr").eq(-1).after(newTR);
                $("#vrsnName").val(item.vrsnName)
                $("#vrsnTypeName").val(item.className)
            })
            $formData.initForm();
        });
    };

    function addOrUpdate(url,callBack){
        var result = $.validator($formData);
        if (result) {
            var params = [];
            $formData.find('select[name="paramId"]').each(function(i,item){
                params.push({
                    name  : "list["+ i + "].paramName",
                    value : $(this).val()
                });
            });
            $formData.find('input[name="paramName"]').each(function(i,item){
                params.push({
                    name  : "list["+ i + "].paramValue",
                    value : $(this).val()
                });
                params.push({
                    name  : "list["+ i + "].sn",
                    value : i
                });
            });
            params.push({
                name  : "vrsnName",
                value : $("#vrsnName").val()
            },{
                name  : "vrsnType",
                value : $("#vrsnType").val()
            });
            $.ajaxExt({
                url : url,
                data : params
            }).done(function(){
                callBack();
            });
        }
    }

    function selectParam(cnt,vrsnType){
        $.ajaxExt({
            url :"/upgrade/getParamByVrsnType.htm",
            data : {'vrsnType' : vrsnType}
        }).done(function(data){
            paramHtml="";
            $(data).each(function(i,item) {
                paramHtml+='<option value="'+item.paramId+'"'+(i===0?"selected":"")+'>'+item.paramLevel2Name+'</option>';
            });
             $("#paramId"+cnt).html(paramHtml);
        });
    }
});